package cn.holy.app.xcddt.ui.activity.setting

import androidx.lifecycle.Observer
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.View
import android.widget.Button
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import cn.holy.app.xcddt.R
import cn.holy.app.xcddt.app.App
import cn.holy.app.xcddt.database.dao.RecordUserDao
import cn.holy.app.xcddt.domain.RecordUserBean
import cn.holy.app.xcddt.repository.RecordRepository
import cn.holy.app.xcddt.ui.activity.base.BaseActivity
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import kotlinx.android.synthetic.main.activity_username.*
import java.util.*
import javax.inject.Inject

/**
 * Created by 69568 on 2018/3/5.
 */
@Route(path = "/app/setting/username")
class RecordUserActivity : BaseActivity() {

    @Inject
    lateinit var reocrdRepo: RecordRepository


    @Inject
    lateinit var reocrdUserDao: RecordUserDao
    lateinit var rightActionButton: Button
    var checkedList: ArrayList<RecordUserBean> = ArrayList()
    lateinit var commonAdapter: CommonAdapter<RecordUserBean>
    private var dataList: ArrayList<RecordUserBean> = ArrayList()


    var isEdit = false

    override fun initData() {
    }

    override fun initView() {
        App.appComponent.inject(this)
        QMUIStatusBarHelper.translucent(this)
        QMUIStatusBarHelper.setStatusBarDarkMode(this)
        top_bar.setTitle("管理巡查人员").setTextColor(ContextCompat.getColor(this, R.color.colorWhite))
        top_bar.addLeftImageButton(R.mipmap.ic_back, R.id.action_close).setOnClickListener({ finish() })
        rightActionButton = top_bar.addRightTextButton(R.string.action_edit, R.id.action_edit)
        rightActionButton.setTextColor(ContextCompat.getColor(this, R.color.colorWhite))
        rightActionButton.setOnClickListener {
            isEdit = !isEdit
            if (isEdit) {
                toolbar.visibility = View.VISIBLE
                rightActionButton.text = "取消"
            } else {
                toolbar.visibility = View.GONE
                rightActionButton.text = "编辑"
            }
            commonAdapter.notifyDataSetChanged()
        }

        val linearLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        recyclerView.layoutManager = linearLayoutManager
        recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))

        commonAdapter = object : CommonAdapter<RecordUserBean>(this, R.layout.layout_username_item, dataList) {
            override fun convert(holder: ViewHolder, userBean: RecordUserBean, position: Int) {

                holder.getView<TextView>(R.id.tv_username).text = userBean.username
                val checkBox = holder.getView<CheckBox>(R.id.checkBox)
                val checkMarker = holder.getView<ImageView>(R.id.checkMarker)
                if (isEdit) {
                    checkMarker.visibility = View.GONE
                    checkBox.isChecked = checkedList.contains(userBean)
                    checkBox.visibility = View.VISIBLE
                } else {
                    checkBox.visibility = View.GONE
                    checkMarker.visibility = if (userBean.select) View.VISIBLE else View.GONE
                }
            }
        }
        commonAdapter.setOnItemClickListener(object : MultiItemTypeAdapter.OnItemClickListener {
            override fun onItemClick(view: View?, holder: RecyclerView.ViewHolder?, position: Int) {
                val username = dataList[position]
                if (isEdit) {
                    if (checkedList.contains(username)) {
                        checkedList.remove(username)
                    } else {
                        checkedList.add(username)
                    }
                    commonAdapter.notifyDataSetChanged()
                }else {
                    username.select = !username.select
                    reocrdUserDao.updateUser(username)
                }
            }

            override fun onItemLongClick(view: View?, holder: RecyclerView.ViewHolder?, position: Int): Boolean {
                return true
            }

        })


        recyclerView.adapter = commonAdapter
        btn_delete.setOnClickListener {

            reocrdUserDao.deleteByUsers(checkedList)
            showTips("删除成功", QMUITipDialog.Builder.ICON_TYPE_SUCCESS)

        }
        btn_upload.setOnClickListener {
            val builder = QMUIDialog.EditTextDialogBuilder(this)
                    .setPlaceholder("请输入巡查人员")
            val editText = builder.editText
            builder.addAction("保存") { dialog, _ ->
                val recordUserBean = RecordUserBean()
                recordUserBean.username = editText.text.toString()
                recordUserBean.select = true
                recordUserBean.create_at = Date()
                reocrdUserDao.save(recordUserBean)

                dialog.dismiss()
            }.addAction("取消") { dialog, _ ->
                dialog.dismiss()
            }.show()
        }
        loadRecordUsername()
    }


    private fun loadRecordUsername() {
        reocrdUserDao.loadRecordUsers().observe(this, Observer<List<RecordUserBean>> { list ->

            dataList.clear()
            list?.let {
                dataList.addAll(it)
            }
            commonAdapter.notifyDataSetChanged()
        })

    }

    override fun initLayout() {
        setContentView(R.layout.activity_username)
    }

}